<template>
  <div class="pro-loading" v-show="msg.show">
    <div class="pro-loading_box">
      <div class="radar-spinner">
        <div class="circle">
          <div class="circle-inner-container">
            <div class="circle-inner"></div>
          </div>
        </div>

        <div class="circle">
          <div class="circle-inner-container">
            <div class="circle-inner"></div>
          </div>
        </div>

        <div class="circle">
          <div class="circle-inner-container">
            <div class="circle-inner"></div>
          </div>
        </div>

        <div class="circle">
          <div class="circle-inner-container">
            <div class="circle-inner"></div>
          </div>
        </div>
      </div>
      <div ref="refMessage" class="radar-spinner_title"></div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { Ref } from 'vue'
  import { init } from 'ityped'
  interface Props {
    msg: { title?: string; show?: boolean }
  }
  const refMessage: Ref<HTMLDivElement | null> = ref(null)

  const { msg = { title: '正在加载中', show: false } } = defineProps<Props>()
  watch(msg, (msgValue) => {
    if (msgValue.show) {
      if (refMessage?.value) {
        refMessage.value.innerHTML = ''
        init(refMessage?.value, {
          showCursor: false,
          loop: true,
          startDelay: 0,
          strings: [msg.title]
        })
      }
    }
  })
</script>

<style lang="less" scoped>
  .pro-loading {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.2);
    z-index: 2009;
  }
  .pro-loading_box {
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
  }
  .radar-spinner_title {
    margin-top: 10px;
    color: var(--primary-color);
    font-weight: 600;
  }
  .radar-spinner,
  .radar-spinner * {
    box-sizing: border-box;
  }

  .radar-spinner {
    height: 60px;
    width: 60px;
    position: relative;
  }

  .radar-spinner .circle {
    position: absolute;
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
    animation: radar-spinner-animation 2s infinite;
  }

  .radar-spinner .circle:nth-child(1) {
    padding: calc(60px * 5 * 2 * 0 / 110);
    animation-delay: 300ms;
  }

  .radar-spinner .circle:nth-child(2) {
    padding: calc(60px * 5 * 2 * 1 / 110);
    animation-delay: 300ms;
  }

  .radar-spinner .circle:nth-child(3) {
    padding: calc(60px * 5 * 2 * 2 / 110);
    animation-delay: 300ms;
  }

  .radar-spinner .circle:nth-child(4) {
    padding: calc(60px * 5 * 2 * 3 / 110);
    animation-delay: 0ms;
  }

  .radar-spinner .circle-inner,
  .radar-spinner .circle-inner-container {
    height: 100%;
    width: 100%;
    border-radius: 50%;
    border: calc(60px * 5 / 110) solid transparent;
  }

  .radar-spinner .circle-inner {
    border-left-color: var(--primary-color);
    border-right-color: var(--primary-color);
  }

  @keyframes radar-spinner-animation {
    50% {
      transform: rotate(180deg);
    }
    100% {
      transform: rotate(0deg);
    }
  }
</style>
